//package com.cnooc.aicv.lmapp.service;
//
//
//import com.cnooc.aicv.lmapp.model.Message;
//import com.cnooc.aicv.lmapp.util.HttpsUtil;
//import lombok.extern.slf4j.Slf4j;
//import okhttp3.Headers;
//import okhttp3.OkHttpClient;
//import okhttp3.Request;
//import okhttp3.Response;
//import org.springframework.stereotype.Service;
////import org.json.JSONException;
////import org.json.JSONObject;
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.io.InputStreamReader;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.nio.charset.StandardCharsets;
//
//@Service
//@Slf4j
//public class HttpRequestExample {
//
//    private OkHttpClient client = HttpsUtil.getUnsafeOkHttpClient().build();
//
//    public static void main(String[] args) {
//        Message message = new Message();
//        //message.setContent("这是一个测试消息"); // 设置消息内容
//
//        try {
//            String url = "http://localhost:8080/test/send"; // 替换为实际的目标URL
//            sendGetRequest(url, message);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    public static void sendGetRequest(String urlString, Message message) throws Exception {
//        // 构建URL
//        String urlWithParams = urlString + "?content=" + java.net.URLEncoder.encode(message.getContent(), StandardCharsets.UTF_8);
//        URL url = new URL(urlWithParams);
//
//        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//        connection.setRequestMethod("GET");
//
//        // 获取响应码
//        int responseCode = connection.getResponseCode();
//        System.out.println("Response Code: " + responseCode);
//
//        // 读取响应体
//        if (responseCode == HttpURLConnection.HTTP_OK) { // 成功
//            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//            String inputLine;
//            StringBuilder response = new StringBuilder();
//
//            while ((inputLine = in.readLine()) != null) {
//                response.append(inputLine);
//            }
//            in.close();
//
//            // 输出完整的响应内容
//            System.out.println("Response Body: " + response.toString());
//        } else {
//            System.out.println("GET request not worked");
//        }
//    }
//
//
//    private void sendRequestAndAggregate(String url, String token,
//                                      Object o) {
//        // 构建请求头，携带 token
//        Headers headers = new Headers.Builder()
//                .add("Authorization", token)
//                .build();
//        // 构建 GET 请求
//        Request request = new Request.Builder()
//                .url(url)
//                .get()  // GET 请求
//                .headers(headers)
//                .build();
////        log.info("request: {}", request);
//        // 执行请求并处理响应
//        try (Response response = client.newCall(request).execute()) {
//            // 判断响应是否成功 (状态码 2xx)
//            if (response.isSuccessful()) {
//                // 处理响应数据
//                String responseString = response.body().string();
////                log.info("Response for url {}: {}", url, responseString);
//
//                // 解析响应 JSON（使用 optInt() 获取字段）
////                JSONObject jsonResponse = new JSONObject(responseString);
////                totalBoxCount.addAndGet(jsonResponse.optInt("boxCount", 0));
////                totalBoxOnlineCount.addAndGet(jsonResponse.optInt("boxOnlineCount", 0));
////                totalBoxOfflineCount.addAndGet(jsonResponse.optInt("boxOfflineCount", 0));
////                totalNvrCount.addAndGet(jsonResponse.optInt("nvrCount", 0));
////                totalNvrOnlineCount.addAndGet(jsonResponse.optInt("nvrOnlineCount", 0));
////                totalNvrOfflineCount.addAndGet(jsonResponse.optInt("nvrOfflineCount", 0));
////                totalCameraCount.addAndGet(jsonResponse.optInt("cameraCount", 0));
////                totalCameraOnlineCount.addAndGet(jsonResponse.optInt("cameraOnlineCount", 0));
////                totalCameraOfflineCount.addAndGet(jsonResponse.optInt("cameraOfflineCount", 0));
//            } else {
//                // 如果响应失败，输出错误信息
//                log.error("Request failed for url {} with status code: {}", url, response.code());
//            }
//        } catch (IOException e) {
//            log.error("Request failed for url {} due to network error: {}", url, e.getMessage(), e);
//        } catch (Exception e) {
//            log.error("Unexpected error for url {}: {}", url, e.getMessage(), e);
//        }
//    }
//
//}